﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

namespace sow
{
	/// <summary>
	/// Summary description for emailcompose.
	/// </summary>
	public partial class emaillistEdit : System.Web.UI.Page
	{
		int EmailListID = -1;
		int UserID;


	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			HttpCookie emailCookie = Request.Cookies["Email"];
			if(emailCookie == null)
				Response.Redirect("index.aspx");

			DataSet ds = EmailDataAccess.GetUser(emailCookie.Value);
			UserID = Convert.ToInt32(ds.Tables[0].Rows[0]["UserID"]);

			if(Request["ListID"] != null)
			{
				EmailListID = Convert.ToInt32(Request["ListID"]);
				ds = EmailDataAccess.GetEmailListByEmailListID(EmailListID);
				this.TextBox_ListName.Text = ds.Tables[0].Rows[0]["ListName"].ToString();
				this.Button_Delete.Visible = true;
				this.Button_Delete.Attributes["onclick"] = "if(!confirm('确实要删除“" + ds.Tables[0].Rows[0]["ListName"].ToString() + "”')){return false}";
			}
			else
			{
				this.Button_Delete.Visible = false;
			}

			this.Button_Save.Attributes["onclick"] = "if(document.all('TextBox_ListName').value==''){alert('请填写列表名称！');document.all('TextBox_ListName').focus();return false}";
			if (!IsPostBack) 
			{
				
				BindListBox();
			}

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		private void BindListBox()
		{
			DataSet ds = EmailDataAccess.GetDataSet("EmailAddress", "NOT EmailID IN (SELECT EmailID FROM EmailAndList WHERE EmailListID=" + EmailListID.ToString() + ")");
			this.ListBox_Left.DataValueField = "EmailID";
			this.ListBox_Left.DataTextField = "EmailAddress";
			this.ListBox_Left.DataSource = ds.Tables[0].DefaultView;
			this.ListBox_Left.DataBind();

			ds = EmailDataAccess.GetEmailAddressByEmailListID(EmailListID);
			this.ListBox_Right.DataValueField = "EmailID";
			this.ListBox_Right.DataTextField = "EmailAddress";
			this.ListBox_Right.DataSource = ds.Tables[0].DefaultView;
			this.ListBox_Right.DataBind();
		}

		protected void Button_Delete_Click(object sender, System.EventArgs e)
		{
			if(EmailListID > 0)
			{
				EmailDataAccess.DeleteEmailList(EmailListID.ToString());
				Response.Redirect("emaillist.aspx");
			}
		}

		protected void Button_Add_Click(object sender, System.EventArgs e)
		{
			if(this.ListBox_Left.SelectedIndex > -1)
			{
				ListItem item = new ListItem(this.ListBox_Left.SelectedItem.Text, this.ListBox_Left.SelectedItem.Value);
				this.ListBox_Right.Items.Add(item);
				this.ListBox_Left.Items.Remove(this.ListBox_Left.SelectedItem);
			}
		}

		protected void Button_Remove_Click(object sender, System.EventArgs e)
		{
			if(this.ListBox_Right.SelectedIndex > -1)
			{
				ListItem item = new ListItem(this.ListBox_Right.SelectedItem.Text, this.ListBox_Right.SelectedItem.Value);
				this.ListBox_Left.Items.Add(item);
				this.ListBox_Right.Items.Remove(this.ListBox_Right.SelectedItem);
			}
		}

		protected void Button_Save_Click(object sender, System.EventArgs e)
		{
			if(EmailListID > 0)
			{
				EmailDataAccess.UpdateEmailList(EmailListID, this.TextBox_ListName.Text.Trim());
				EmailDataAccess.DeleteEmailAndListByEmailListID(EmailListID.ToString());
			}
			else
			{
				EmailListID = EmailDataAccess.InsertEmailList(UserID, this.TextBox_ListName.Text.Trim());
			}

			foreach(ListItem item in this.ListBox_Right.Items)
			{
				EmailDataAccess.InsertEmailAndList(EmailListID, Convert.ToInt32(item.Value));
			}

			Response.Redirect("emaillist.aspx?ListID=" + EmailListID.ToString());
		}

		

		
		

	}



}
